package com.yami.shop.seckill.common.listener;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yami.shop.bean.event.CategoryWordEvent;
import com.yami.shop.seckill.common.enums.SeckillEnum;
import com.yami.shop.seckill.common.model.Seckill;
import com.yami.shop.seckill.common.service.SeckillService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("CategorySeckillListener")
@Slf4j
@AllArgsConstructor
public class CategorySeckillListener {
    private final SeckillService seckillService;

    @EventListener(CategoryWordEvent.class)
    public void categorySeckillListener(CategoryWordEvent event){
        List<Long> prodIds = event.getProdIdList();
        // 失效秒杀活动
        seckillService.update(Wrappers.lambdaUpdate(Seckill.class)
                .set(Seckill::getStatus, SeckillEnum.INVALID.getValue())
                .in(Seckill::getProdId, prodIds)
                .eq(Seckill::getStatus, SeckillEnum.NORMAL.getValue())
        );
    }

}
